package com.dts.dao;

import com.dts.entity.Good;
import com.dts.utils.PoolUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodDao {

    public static List<Good> getGoods() {
        Connection conn = PoolUtils.getConnection();
        List<Good> list = new ArrayList<>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("select * from good");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Good good = new Good(rs.getInt("id"), rs.getString("name"), rs.getDouble("price"), rs.getString("description"), rs.getDate("create_time"));
                list.add(good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static Good getGoodById(int id) {
        Connection conn = PoolUtils.getConnection();
        Good good = null;
        try {
            PreparedStatement pstmt = conn.prepareStatement("select * from good where id = ?");
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                good = new Good(rs.getInt("id"), rs.getString("name"), rs.getDouble("price"), rs.getString("description"), rs.getDate("create_time"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return good;
    }

    public static void addGood(Good good) {
        Connection conn = PoolUtils.getConnection();
        try {
            PreparedStatement pstmt = conn.prepareStatement("insert into good (name, price, description, create_time) values (?, ?, ?, ?)");
            pstmt.setString(1, good.getName());
            pstmt.setDouble(2, good.getPrice());
            pstmt.setString(3, good.getDescription());
            pstmt.setDate(4, new java.sql.Date(good.getCreateTime().getTime()));
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updateGood(Good good) {
        Connection conn = PoolUtils.getConnection();
        try {
            PreparedStatement pstmt = conn.prepareStatement("update good set name = ?, price = ?, description = ?, create_time = ? where id = ?");
            pstmt.setString(1, good.getName());
            pstmt.setDouble(2, good.getPrice());
            pstmt.setString(3, good.getDescription());
            pstmt.setDate(4, new java.sql.Date(good.getCreateTime().getTime()));
            pstmt.setInt(5, good.getId());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteGood(int id) {
        Connection conn = PoolUtils.getConnection();
        try {
            PreparedStatement pstmt = conn.prepareStatement("delete from good where id = ?");
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
